<template>
  <CommonPage :show-header="false">
    <article class="margin-top-sm flex  flex-column flex-sub">
      <section style="display: flex;justify-content: space-between;margin-bottom: 10px">
        <div style="display: flex">
          <SortSelect3 v-model:value="sortKey" @on-select="search"></SortSelect3>
        </div>
        <div style="display: flex">
          <SearchInput v-model:value="searchKey" @on-change="search" placeholder="ID" :loading="loading"></SearchInput>
        </div>
      </section>
      <section  style="overflow:hidden" class="flex flex-sub radius-lg bg-white  padding-tb-sm">
        <CurdTable ref="tableRef"
                   :get-data="api.comment.commentRootQueryList"
                   :columns="columns"
                   :and-params="andParams"
                   :or-params="orParams"
                   :order-params="orderParams">
        </CurdTable>
      </section>
    </article>
  </CommonPage>
</template>

<script setup lang="ts">
import CommonPage from "@/components/page/CommonPage.vue";
import CurdTable from "@/components/common/table/CurdTable.vue";
import {h, onMounted, ref} from "vue";
import api from '@/api'
import SortSelect3 from "@/components/example/select/SortSelect3.vue";
import SearchInput from "@/components/common/input/SearchInput.vue";
import { NButton, NEllipsis, NText} from "naive-ui";
import {useClipboard} from "@vueuse/core";
import {
  getCommentStatus,
  getCommentStatusColor,
  getCommentType
} from "@/utils";
import myMoment from "@/utils/common/myMoment";

import router from "@/router";
import {useRoute} from "vue-router";


const {copy} = useClipboard()

/** QueryBar筛选参数（可选） */
const orParams = ref({
  id: '',
})
/** 补充参数（可选） */
const andParams = ref({
  id:'',
})
/** 排序参数(可选）*/
const orderParams = ref({
  createTime: '',
})
const loading = ref()
const tableRef = ref()
const searchKey = ref('')
const sortKey = ref('2')

const route =useRoute()
const columns = ref([
  {title: 'ID', key: 'id', width: 2, ellipsis: {tooltip: true}, align: 'center',},
  {
    title: '评论用户', key: 'fromUsername', width: 4, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.fromUsername})
    }
  },
  {
    title: '评论类型', key: 'targetType', width:4, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => getCommentType(row.targetType)})
    }
  },
  {
    title: '目标用户', key: 'toUsername', width: 4, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.toUsername})
    }
  },

  {
    title: '点赞', key: 'likeCount', width: 2, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.likeCount})
    }
  },

  {
    title: '评论时间', key: 'createTime', width: 5, ellipsis: {tooltip: true}, align: 'center',
    render(row: { [x: string]: any; }) {
      return h("div", {}, [
        h("div", {style: {fontSize: '14px'}}, [h(NEllipsis, {}, {default: () => myMoment(row?.createTime).format("MMMDo H:mm")})]),
        h(NText, {depth: '3'}, {default: () => myMoment(row?.createTime).startOf('minute').fromNow()}),
      ])
    }
  },
  {
    title: '内容', key: 'content', width: 8, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {style: {fontSize: '14px'}}, {default: () => row.content})
    }
  },
  {
    title: '状态', key: 'status', width: 2, ellipsis: {tooltip: true}, align: 'center',
    render(row: any) {
      return h(NText, {
        style: {
          fontSize: '14px',
          color: getCommentStatusColor(row.status)
        }
      }, {default: () => getCommentStatus(row.status)})
    }
  },

  {
    title: '操作', key: 'actions', width: 5, align: 'center',
    render(row: any) {
      return [
        h(NButton, {
          round: true,
          secondary: true,
          size: 'small',
          type: "info",
          onClick: () => handleQueryReply(row)
        }, {default: () => "查看回复"}),
        h(NButton, {
          round: true,
          secondary: true,
          size: 'small',
          type: "error",
          onClick: () => handleDelete(row)
        }, {default: () => "删除"}),
      ]
    }
  }
])

onMounted(() => {
  if(route.query.id){
    //@ts-ignore
    andParams.value.id=route.query.id
  }
  search()
  andParams.value.id=''
})


const search = async () => {
  loading.value = true
  Object.keys(orParams.value).forEach((key: any) => {
    //@ts-ignore
      orParams.value[key] = `%${searchKey.value}%`
  })
  if (sortKey.value === '1') {
    orderParams.value.createTime = '1'
  } else if (sortKey.value === '2') {
    orderParams.value.createTime = '2'
  } else {
    orderParams.value.createTime = ''
  }
  await tableRef.value.handleSearch()
  loading.value = false
}




const handleQueryReply = (row: any) => {
  router.push({
    path:'/interaction/reply',
    query:{
      rootId:row.id
    }
  })
}
const handleDelete = (row: any) => {
  window.$dialog.error({
    title: '确定要删除此评论吗？',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: () => {
      api.comment.removeCommentById({
        id:row.id
      }).then(res => {
        if (res && res.data.code === 0) {
          window.$message.success(res.data.msg)
        }
      })
    }
  })
}


</script>

<style scoped>

</style>